package service

import (
	pb "blog/api/blog/v1"
	"blog/internal/biz"
	"context"
	"github.com/go-kratos/kratos/v2/log"
)

type BlogService struct {
	pb.UnimplementedBlogServer
	ar  *biz.ArticleUsecase
	log *log.Helper
}

func NewBlogService(ar *biz.ArticleUsecase, logger log.Logger) *BlogService {
	return &BlogService{ar: ar, log: log.NewHelper(logger)}
}

func (s *BlogService) CreateArticle(ctx context.Context, req *pb.CreateArticleRequest) (*pb.CreateArticleReply, error) {
	return &pb.CreateArticleReply{}, nil
}
func (s *BlogService) UpdateArticle(ctx context.Context, req *pb.UpdateArticleRequest) (*pb.UpdateArticleReply, error) {
	return &pb.UpdateArticleReply{}, nil
}
func (s *BlogService) DeleteArticle(ctx context.Context, req *pb.DeleteArticleRequest) (*pb.DeleteArticleReply, error) {
	return &pb.DeleteArticleReply{}, nil
}
func (s *BlogService) GetArticle(ctx context.Context, req *pb.GetArticleRequest) (*pb.GetArticleReply, error) {
	return &pb.GetArticleReply{}, nil
}
func (s *BlogService) ListArticle(ctx context.Context, req *pb.ListArticleRequest) (*pb.ListArticleReply, error) {
	list, err := s.ar.List(ctx)
	if err != nil {
		return nil, err
	}

	// 把数据转换成protobuf格式的
	articleList := make([]*pb.Article, 0)
	for _, article := range list {
		articleList = append(articleList, &pb.Article{
			Id:      article.ID,
			Title:   article.Title,
			Content: article.Content,
			Like:    article.Like,
		})
	}

	return &pb.ListArticleReply{Results: articleList}, nil
}
